#ifndef COURSE_LIB_H_
#define COURSE_LIB_H_
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <vector>
#include "Course.h"
#include "DataRow.h"

void Add_course(string s, Course l[], int &numberCourse){
    string temp, a_sid, a_lid, a_room, a_time;
    short a_sem;
    int mark=0;

    mark = temp.find("::");
    a_sid = temp.substr(0,mark);

    temp = temp.substr(mark+2);
    mark = temp.find("::");
    a_lid = temp.substr(0,mark);

    temp = temp.substr(mark+2);
    mark = temp.find("::");
    a_sem = atoi(temp.substr(0,mark).c_str());

    temp = temp.substr(mark+2);
    mark = temp.find("::");
    a_room = temp.substr(0,mark);

    temp = temp.substr(mark+2);
    mark = temp.find("::");
    a_time = temp.substr(0,mark);

    Course Course(a_sid, a_lid, a_sem, a_room, a_time);
    l[numberCourse] = Course;
    numberCourse++;
}

void ReadCourse(string _file, Course l[], int &numberCourse){
    ifstream fin;
               fin.open(_file.c_str());
               if(!fin.is_open()){
                       cout<<"Cannot open file.\n";
                       exit(1);
               }else{
                              string s = "";
                              while (getline(fin, s)){
                                Add_course(s, l, numberCourse);
                              }
                              fin.close();
               }
}

int CheckConflictRoom(Course l[], int &i){
    int j,k = 0, ret = 0;
    string room[100], time[100];
    for (j=0; j<i;j++){
        room[j] = l[j].getRoom();
        time[j] = l[j].getTime();
    }
    for (j=0; j<i; j++){
        for (k=j; k < i; k++){
            if (j != k){
                if ((room[j] == room[k]) && (time[j] == time[k])){
                    string c1 = l[j].getCourseID();
                    string c2 = l[k].getCourseID();
                    cout << "Course "<<c1<<" vs. "<<" Course "<<c2<<" have same room!\n";
					ret = 1;
                }
            }
        }
    }
	return ret;
}

int CheckConflictLecturer(Course l[], int &i){
    int j,k = 0, ret = 0;
    string lec[100], time[100];
    for (j=0; j<i;j++){
        lec[j] = l[j].getLecturerID();
        time[j] = l[j].getTime();
    }

    for (j=0; j<i; j++){
        for (k=j; k < i; k++){
            if (j != k){
                if ((lec[j] == lec[k]) && (time[j] == time[k])){
                    string c1 = l[j].getCourseID();
                    string c2 = l[k].getCourseID();
                    cout << "Course "<<c1<<" vs. "<<" Course "<<c2<<" have same lecturer!\n";
					ret = 1;
                }
            }
        }
    }
	return ret;
}

vector<DataRow> getCourseByLecturerName(Course *c, int size, string lec_name){
	int i = 0, count = 0;
	vector<DataRow> vect;
	sortSubjectByTime(c, size);

	for(i; i < size; i++){
		if(c[i].getLecturerID()){
			DataRow d(inp[i], count);
			vect.push_back(d);
		}
	}
	return vect;
}

void getTimePara(Course c, string &start, string &end, int &day){
	string t = c.getTime(), day_temp;
	int mark = 0;
	mark = t.find("-");
	start = t.substr(0,mark);

	t = t.substr(mark+1);
	mark = t.find("-");
	end = t.substr(0,mark);

	t = t.substr(mark+1);
	day = atoi(t.c_str());
}

void getSubjectsBySem(int sem, Course *inp, int size_i, Course *out, int &size_o){
	int i = 0, count = 0;
	for(i; i < size_i; i++){
		if(sem == inp[i].getSem()){
			out[count] = inp[i];
			count++;
		}
	}
	size_o = count;
}

int getHourFromString(string s){
	int mark = s.find(":");
	return atoi(s.substr(0,mark).c_str());
}

int getMinuteFromString(string s){
	int mark = s.find(":");
	return atoi(s.substr(mark, s.length()).c_str());
}

void swap(Course &a, Course &b){
	Course temp;
	temp = a;
	a = b;
	b = temp;
}

void sortSubjectByTime(Course *inp, int size_i){
	int i = size_i-1, day, day_l;
	float temp1, temp2;
	string start, end, start_l, end_l;
	Course c;
	for(i; i>0; i--)
	{
		getTimePara(inp[i], start, end, day);
		getTimePara(inp[i-1], start_l, end_l, day_l);
		temp1 = getHourFromString(start) + getMinuteFromString(start)/60;
		temp2 = getHourFromString(start_l) + getMinuteFromString(start_l)/60;
		if(day == day_l)
		{if(temp1 < temp2){
			swap(inp[i], inp[i-1]);
			i = size_i-1;
			continue;
		}
		}else if(day < day_l){
			swap(inp[i], inp[i-1]);
			i = size_i-1;
			continue;
		}
	}
}

void printCalendar(Course *inp, int size, short sem){
	int i = 0;
	sortSubjectByTime(inp, size);
	for(i; i < size; i++){
		if(inp[i].getSem() == sem){
			cout<<inp[i].getInfo()<<"\n";
		}
	}
}


vector<DataRow> getCalendarVector(Course *inp, int size, short sem){
	int i = 0, count = 1;
	vector<DataRow> vect;
	sortSubjectByTime(inp, size);
	for(i; i < size; i++){
		if(inp[i].getSem() == sem){
			DataRow d(inp[i], count);
			vect.push_back(d);
			count++;
		}
	}
	return vect;
}

Course GetCourse(string sid, int lid){
    int i = 0, j = 0;
    Course l[100];
    ReadCourse("schedule.txt", l, i);

    for (j = 0; j < i; j++){
        string c_sid = l[j].getSubjectID();
        int c_lid = atoi(l[j].getLecturerID().c_str());

        if ((sid == c_sid) && (lid == c_lid)){
            return l[j];
        }
    }
    Course c("", "0", 0, "", "");
    return c;
}
	
void getCal(Course *inp, int size){
	int i = 0;
	sortSubjectByTime(inp, size);
	for(i; i < size; i++){
		cout<<inp[i].getInfo()<<"\n";
	}
}

#endif // COURSE_LIB_H_
